package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.logging.Logger;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.LuntaiEquipment;
import com.ruoyi.system.domain.LuntaiOrder;
import com.ruoyi.system.domain.LuntaiPolicyExtensionRequest;
import com.ruoyi.system.domain.dto.LuntaiEquipmentUseLogDto;
import com.ruoyi.system.mapper.LuntaiEquipmentMapper;
import com.ruoyi.system.mapper.LuntaiOrderMapper;
import com.ruoyi.system.mapper.LuntaiPolicyExtensionRequestMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.LuntaiEquipmentUseLogMapper;
import com.ruoyi.system.domain.LuntaiEquipmentUseLog;
import com.ruoyi.system.service.ILuntaiEquipmentUseLogService;

/**
 * 器材使用记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-16
 */
@Service
public class LuntaiEquipmentUseLogServiceImpl implements ILuntaiEquipmentUseLogService {
    @Autowired
    private LuntaiEquipmentUseLogMapper luntaiEquipmentUseLogMapper;

    @Autowired
    private LuntaiEquipmentMapper luntaiEquipmentMapper;

    @Autowired
    private LuntaiOrderMapper luntaiOrderMapper;
    @Autowired
    private LuntaiPolicyExtensionRequestMapper luntaiPolicyExtensionRequestMapper;


    /**
     * 查询器材使用记录
     *
     * @param id 器材使用记录主键
     * @return 器材使用记录
     */
    @Override
    public LuntaiEquipmentUseLog selectLuntaiEquipmentUseLogById(Integer id) {
        return luntaiEquipmentUseLogMapper.selectLuntaiEquipmentUseLogById(id);
    }

    /**
     * 查询器材使用记录列表
     *
     * @param luntaiEquipmentUseLog 器材使用记录
     * @return 器材使用记录
     */
    @Override
    public List<LuntaiEquipmentUseLog> selectLuntaiEquipmentUseLogList(LuntaiEquipmentUseLog luntaiEquipmentUseLog) {
        return luntaiEquipmentUseLogMapper.selectLuntaiEquipmentUseLogList(luntaiEquipmentUseLog);
    }

    @Override
    public List<LuntaiEquipmentUseLogDto> selectLuntaiEquipmentUseLogList2(LuntaiEquipmentUseLogDto luntaiEquipmentUseLogDto) {
        return luntaiEquipmentUseLogMapper.selectLuntaiEquipmentUseLogList2(luntaiEquipmentUseLogDto);
    }

    @Override
    public List<LuntaiEquipmentUseLogDto> selectLuntaiEquipmentUseLogList3(Integer requestId) {
        return luntaiEquipmentUseLogMapper.selectLuntaiEquipmentUseLogList3(requestId);
    }

    /**
     * 新增器材使用记录
     *
     * @param luntaiEquipmentUseLog 器材使用记录
     * @return 结果
     */
    @Override
    public int insertLuntaiEquipmentUseLog(List<LuntaiEquipmentUseLog> luntaiEquipmentUseLogs) {
        Integer requestId = luntaiEquipmentUseLogs.get(0).getRequestId();
        List<LuntaiEquipmentUseLog> logs = luntaiEquipmentUseLogMapper.selectLuntaiEquipmentUseLogByRequestId(requestId);
        for (LuntaiEquipmentUseLog log : logs) {
            LuntaiEquipmentUseLog luntaiEquipmentUseLog = new LuntaiEquipmentUseLog();
            luntaiEquipmentUseLog.setEquipmentId(log.getEquipmentId());
            luntaiEquipmentUseLog.setNum(log.getNum());
            luntaiEquipmentUseLog.setType(1);
            luntaiEquipmentMapper.modifyNum(luntaiEquipmentUseLog);
        }
        luntaiEquipmentUseLogMapper.deleteLogByRequestId(requestId);
        for (LuntaiEquipmentUseLog luntaiEquipmentUseLog : luntaiEquipmentUseLogs) {
            luntaiEquipmentUseLog.setCreateTime(DateUtils.getNowDate());
            luntaiEquipmentUseLog.setUserId(Math.toIntExact(SecurityUtils.getUserId()));
            luntaiEquipmentUseLogMapper.insertLuntaiEquipmentUseLog(luntaiEquipmentUseLog);
            luntaiEquipmentMapper.modifyNum(luntaiEquipmentUseLog);
        }
        LuntaiPolicyExtensionRequest request = luntaiPolicyExtensionRequestMapper.selectLuntaiPolicyExtensionRequestById(requestId);
        luntaiOrderMapper.updateOrderByTypeAndSourceId(request.getType(), request.getId(), Math.toIntExact(SecurityUtils.getUserId()));
        return 1;
    }

    /**
     * 修改器材使用记录
     *
     * @param luntaiEquipmentUseLog 器材使用记录
     * @return 结果
     */
    @Override
    public int updateLuntaiEquipmentUseLog(LuntaiEquipmentUseLog luntaiEquipmentUseLog) {
//        LuntaiEquipmentUseLog userLog = new LuntaiEquipmentUseLog();

//        luntaiEquipmentUseLogMapper.updateLuntaiEquipmentUseLogById(l)
        return luntaiEquipmentUseLogMapper.updateLuntaiEquipmentUseLog(luntaiEquipmentUseLog);
    }

    /**
     * 批量删除器材使用记录
     *
     * @param ids 需要删除的器材使用记录主键
     * @return 结果
     */
    @Override
    public int deleteLuntaiEquipmentUseLogByIds(Integer[] ids) {
        return luntaiEquipmentUseLogMapper.deleteLuntaiEquipmentUseLogByIds(ids);
    }

    /**
     * 删除器材使用记录信息
     *
     * @param id 器材使用记录主键
     * @return 结果
     */
    @Override
    public int deleteLuntaiEquipmentUseLogById(Integer id) {
        return luntaiEquipmentUseLogMapper.deleteLuntaiEquipmentUseLogById(id);
    }


}
